import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import HelloWorld from '@/components/HelloWorld'
import { Message } from 'element-ui'
Vue.use(Router)

// 配置axios 实现跨域请求3000端口下的node服务器,安装axios:npm install --save axios
import axios from 'axios'//引入axios
axios.defaults.withCredentials = false; //跨域保存session有用
// axios.defaults.baseURL = "http://localhost:3000";
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.baseURL = "http://localhost:8091/";
//在vue实例或组件中不用再去重复引用Axios 直接用this.$axios就能执行axios 方法
Vue.prototype.$axios = axios;//修改Vue的原型属性
//==============================================axios拦截器
axios.interceptors.request.use(
  config => {
    // if (config.url === "/users/login") {
    if (config.url === "/concert2/user.action") {
      return config
    } else {
      var token = sessionStorage.getItem('token');
      config.headers.token=token;
      return config
    }
  }
);
axios.interceptors.response.use(
  response => {
    return response;
  },
  err => {

    if (error.response.status === 504 || error.response.status === 404) {
      Message.error({message: '服务器被吃了⊙﹏⊙∥'})
      console.error('服务器被吃了⊙﹏⊙∥, ', error)
    } else if (error.response.status === 403) {
      Message.error({message: '权限不足,请联系管理员!'})
      console.error('权限不足,请联系管理员!', error)
    } else if (error.response.status === 401) {
      lstore.removeLStore(types.USER)
      lstore.removeLStore(types.TOKEN)
      Router.push({path: '/login'})
    } else {
      Message.error({message: '未知错误!'})
      console.error('服务端发生错误, ', error)
    }
    if (err.response) {
      router.replace({
        path: 'login',
        query: {
          redirect: router.currentRoute.fullPath
        }
      })

    }
  }
)
//end

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login
    },
    {
     path:'/info',
     name:'info',
     // meta: {
     //   requireAuth: true, // 判断是否需要登录
     //  },
     component: HelloWorld
   }
  ]
})
